#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time    : 2024/11/26 12:34
# @Author  : zmz
# @File    : capital_equivalent.py
# @function: 资金等值计算
# @Software: PyCharm

import PySimpleGUI as sg
import numpy_financial as npf

import debug_infomation as _debug

# 资金的等值计算布局
layout = [
    [sg.Text("   利率(i)   ",),sg.Text("   期数(n)   "),sg.Text("   年值(A)   "),sg.Text("   现值(P)   "),sg.Text("   终值(F)   ")],
    [sg.InputText("0.03", key="I_INPUT", size=(10, 1)),
     sg.InputText("5", key="N_INPUT", size=(10, 1)),
     sg.InputText("0", key="A_INPUT", size=(10, 1)),
     sg.InputText("10000", key="P_INPUT", size=(10, 1)),
     sg.InputText("", key="F_INPUT", size=(10, 1))],
    [sg.Text(" (利率以小数形式输入，要计算的变量输入为空，输入其余四个变量) ")],
    [sg.Button("计算", key="INAPV_BTN")]
]

# 资金等值计算处理
def capital_equivalence_handle( values):
    para_number = 0
    if values['I_INPUT'] != '':
        i = float(values["I_INPUT"])
        para_number += 1
    if values['N_INPUT'] != '':
        n = float(values["N_INPUT"])
        para_number += 1
    if values['A_INPUT'] != '':
        a = float(values["A_INPUT"])
        para_number += 1
    if values['P_INPUT'] != '':
        p = float(values["P_INPUT"])
        para_number += 1
    if values['F_INPUT'] != '':
        f = float(values["F_INPUT"])
        para_number += 1
    if para_number < 4:
        sg.popup("输入参数不足")
        return
        # break
    if para_number == 5:
        sg.popup("目标计算参数不需要输入值")
        return
        # break
    _value = 0.0
    if values['I_INPUT'] == '':
        _value = npf.rate(n, a, p, f)
    elif values['N_INPUT'] == '':
        _value = npf.nper(i, a, p, f)
    elif values['A_INPUT'] == '':
        _value = npf.pmt(i, n, p, f)
    elif values['P_INPUT'] == '':
        _value = npf.pv(i, n, a, f)
    elif values['F_INPUT'] == '':
        _value = npf.fv(i, n, a, p)
    else:
        _value = 0
    print('INAPV_VALUE：', round(_value, 2))
    temp_value = "{:.3f}".format(_value)
    _debug.info('资金等值计算结果为：{}'.format(temp_value))
    # window['-STATUS-'].update(f"结果为: { float(_value) }")  # 更新状态栏
